#!/usr/bin/python
# -*- coding: UTF-8 -*-

from email.mime.text import MIMEText
import smtplib
import urllib2
import time

__metaclass__ = type
class temperature:
   
    def __init__(self, path):
        super(temperature, self).__init__()
        self.path = path

    def getTempture(self):
        try:
            f = open(self.path)
            text = f.readlines()[1]
            f.close()
            return float(text.split('=')[1]) / 1000
        except IOError:
            return 'error'
    
    def sendEmail(self):
        from_addr = '799648397@qq.com'  # 输入邮箱地址和口令
        password = 'mjwnjayqpfbtbbfd'
        to_addr = 'simplismvip@163.com'  # 输入邮箱收件人地址
        smtp_server = 'smtp.qq.com'  # 输入SMTP服务器地址
        #获取当前硬件读取的温度文本
        
        temperatureTur = self.getTempture()
        msgContent = '早上好! 当前室内温为%s 摄氏度。' % temperatureTur
        msg = MIMEText(msgContent, 'plain', 'utf-8')
        msg['Subject'] = '当前室内温度'
        msg['Content-Type'] = 'Text/HTML'
        msg['From'] = from_addr
        msg['To'] = to_addr
        try:
            server = smtplib.SMTP_SSL(smtp_server, 465)
            server.set_debuglevel(1)
            server.login(from_addr, password)
            server.sendmail(from_addr, [to_addr], msg.as_string())
            server.quit()
            print 'Send Success'
        except smtplib.SMTPException, e:
            print 'Send Failed, %s' % e

    def getWeather(self, url):
        # http://api.map.baidu.com/telematics/v3/weather?location=上海&output=JSON&ak=FK9mkfdQsloEngodbFl4FeY3
        user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        header = {'User-Agent': user_agent}
        requ = urllib2.Request(url, headers=header)
        responds = urllib2.urlopen(requ)
        con = responds.read().decode('utf-8')
        print con

    def run(self):
        while True:
            time.sleep(14400)
            self.sendEmail()

t = temperature('/sys/bus/w1/devices/28-0316701572ff/w1_slave')
t.run()



